//
//  RunViewController.m
//  B
//
//  Created by lanou on 16/5/5.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#define kRunURl @"http://app.dunkhome.com/v2/news_v3?c=158&slide=1"

#import "RunViewController.h"

@interface RunViewController ()<UITableViewDataSource, UITableViewDelegate>

// 存放所有的数据
@property (nonatomic, strong) NSMutableArray *dataSourceArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RunViewController
// 懒加载
- (NSMutableArray *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceArr;
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:kRunURl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *runArr = responseObject[@"table_view_data"];
        for (NSDictionary *dic in runArr) {
            RunModel *model = [[RunModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败：%@", error);
    }];
    self.tableView.rowHeight = 120;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:@"RunTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([RunModel class])];
    
    [self.view addSubview:self.tableView];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RunModel *model = [self.dataSourceArr objectAtIndex:indexPath.row];
    BaseTableViewCell *cell = [TableViewCellFactory cellWithModelClass:[model class] tableView:self.tableView index:indexPath];
    [cell setDataWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    RunModel *model = [self.dataSourceArr objectAtIndex:indexPath.row];
    RunDetailViewController *detail = [RunDetailViewController new];
    BaseNavigationController *nav = self.view.window.rootViewController.childViewControllers[1];
    detail.RunID = model.ID.stringValue;
    [nav pushViewController:detail animated:YES];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
